package com.aizuda.boot.modules.business.counterparty.controller;

import com.aizuda.boot.modules.business.contract.dto.SingleResponse;
import com.aizuda.boot.modules.common.constant.enums.*;
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 交易方枚举相关接口
 */
@Slf4j
@RestController
@RequestMapping("/counterparty/enum")
@Tag(name = "交易方枚举相关接口")
public class CounterpartyEnumController {

    @GetMapping("/nature")
    @Operation(summary = "获取交易方性质列表")
    public SingleResponse<List<Map<String, Object>>> getCounterpartyNatureTypes() {
        List<Map<String, Object>> result = new ArrayList<>();

        for (CounterpartyNatureEnum nature : CounterpartyNatureEnum.values()) {
            Map<String, Object> item = new HashMap<>();
            item.put("code", nature.getCode());
            item.put("name", nature.getName());
            item.put("desc", nature.getDesc());
            result.add(item);
        }

        return SingleResponse.of(result);
    }

    @GetMapping("/type")
    @Operation(summary = "获取交易方类型列表")
    public SingleResponse<List<Map<String, Object>>> getCounterpartyTypes() {
        List<Map<String, Object>> result = new ArrayList<>();

        for (CounterpartyTypeEnum type : CounterpartyTypeEnum.values()) {
            Map<String, Object> item = new HashMap<>();
            item.put("code", type.getCode());
            item.put("name", type.getDesc());
            result.add(item);
        }

        return SingleResponse.of(result);
    }

    @GetMapping("/certificate/{natureCode}")
    @Operation(summary = "根据交易方性质获取证件类型列表")
    public SingleResponse<List<Map<String, Object>>> getCertificateTypesByNature(
            @PathVariable @Parameter(description = "交易方性质代码", required = true) Integer natureCode) {

        List<Map<String, Object>> result = new ArrayList<>();

        // 获取对应的证件类型列表
        List<CertificateTypeEnum> certificateTypes = CertificateTypeEnum.getByNatureCode(natureCode);

        for (CertificateTypeEnum certType : certificateTypes) {
            Map<String, Object> item = new HashMap<>();
            item.put("code", certType.getCode());
            item.put("name", certType.getName());
            item.put("desc", certType.getDesc());
            result.add(item);
        }

        return SingleResponse.of(result);
    }

    @GetMapping("/certificate")
    @Operation(summary = "根据国家和交易方性质获取证件类型列表")
    public SingleResponse<List<Map<String, Object>>> getCertificateTypesByCountryAndNature(
            @RequestParam @Parameter(description = "国家代码", required = true) String countryCode,
            @RequestParam @Parameter(description = "交易方性质代码", required = true) Integer natureCode) {

        List<Map<String, Object>> result = new ArrayList<>();

        // 根据国家代码和交易方性质获取对应的证件类型列表
        List<CertificateTypeEnum> certificateTypes = CertificateTypeEnum.getByCountryAndNature(countryCode, natureCode);

        for (CertificateTypeEnum certType : certificateTypes) {
            Map<String, Object> item = new HashMap<>();
            item.put("code", certType.getCode());
            item.put("name", certType.getName());
            item.put("desc", certType.getDesc());
            result.add(item);
        }

        return SingleResponse.of(result);
    }

    @Operation(summary = "获取风险类型枚举值")
    @GetMapping("/getRiskType")
    public SingleResponse<List<JSONObject>> getRiskType() {
        ArrayList<JSONObject> list = new ArrayList<>();
        RiskTypeEnum[] values = RiskTypeEnum.values();
        for (RiskTypeEnum value : values) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", value.getCode());
            jsonObject.put("name", value.getDescription());
            list.add(jsonObject);
        }
        return SingleResponse.of(list);
    }

    @Operation(summary = "获取纳税人类型")
    @GetMapping("/taxpayerType")
    public SingleResponse<List<JSONObject>> getTaxpayerType() {
        List<JSONObject> list = new ArrayList<>();
        for (TaxpayerTypeEnum value : TaxpayerTypeEnum.values()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", value.getCode());
            jsonObject.put("name", value.getDescription());
            list.add(jsonObject);
        }
        return SingleResponse.of(list);
    }
} 